﻿using BO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.system.Right
{
    public partial class edit : System.Web.UI.Page
    {
        Maticsoft.BLL.Rights role = new Maticsoft.BLL.Rights();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidid.Value = Request.QueryString["id"];
                this.parentlist.DataSource = role.GetList("ltype=1");
                this.parentlist.DataTextField = "rightname";
                this.parentlist.DataValueField = "code";
                this.parentlist.DataBind();
                this.parentlist.Items.Insert(0, new ListItem("--请选择所属上级--", ""));
                this.cklist.DataSource = ToSelectListItem(extension.查询);
                this.cklist.DataTextField = "Text";
                this.cklist.DataValueField = "Value";
                this.cklist.DataBind();
                if (!string.IsNullOrEmpty(hidid.Value))
                {
                    Maticsoft.Model.Rights md = role.GetModel(Convert.ToInt32(hidid.Value));
                    name.Value = md.rightname;
                    code.Value = md.code;
                    url.Value = md.url;
                    icon.Value = md.icon;
                    sort.Value = md.sort.ToString();
                    description.Value = md.description;
                    this.parentlist.SelectedValue = md.parentcode;
                    foreach (ListItem item in cklist.Items)
                    {
                        if (md.extension.Split('|').Contains(item.Value))
                        {
                            item.Selected = true;
                        }
                    }
                }
            }

        }
        public static List<ListItem> ToSelectListItem(Enum valueEnum)
        {
            return (from int value in Enum.GetValues(valueEnum.GetType())
                    select new ListItem
                    {
                        Text = Enum.GetName(valueEnum.GetType(), value),
                        Value = value.ToString()
                    }).ToList();
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            Maticsoft.Model.Rights md = new Maticsoft.Model.Rights();
            if (!string.IsNullOrEmpty(hidid.Value))
            {
                md = role.GetModel(Convert.ToInt32(Request["id"]));
                md.code = code.Value;
                md.description = description.Value;
                md.extension = string.Empty;
                foreach (ListItem item in cklist.Items)
                {
                    if (item.Selected)
                    {
                        if (string.IsNullOrEmpty(md.extension))
                            md.extension = item.Value;
                        else
                            md.extension += "|" + item.Value;
                    }
                }
                md.icon = icon.Value;
                md.ltype = string.IsNullOrEmpty(parentlist.SelectedItem.Value) ? 1 : 2;
                md.parentcode = parentlist.SelectedItem.Value;
                md.rightname = name.Value;
                md.sort = Convert.ToInt32(string.IsNullOrEmpty(sort.Value)?0:Convert.ToInt32(sort.Value));
                md.url = url.Value;
                bool row = role.Update(md);
                if (row)
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "  <script>parent.layer.alert(\"修改成功\", function () { parent.location.reload();}); </script>");
                else
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "", " <script>parent.layer.msg(\"修改失败\"); </script>");
            }
            else
            {
                md.code = code.Value;
                md.description = description.Value;
                foreach (ListItem item in cklist.Items)
                {
                    if (item.Selected)
                    {
                        if (string.IsNullOrEmpty(md.extension))
                            md.extension = item.Value;
                        else
                            md.extension += "|" + item.Value;
                    }
                }
                md.icon = icon.Value;
                md.ltype = string.IsNullOrEmpty(parentlist.SelectedItem.Value) ? 1 : 2;
                md.parentcode = parentlist.SelectedItem.Value;
                md.rightname = name.Value;
                md.sort = Convert.ToInt32(string.IsNullOrEmpty(sort.Value) ? 0 : Convert.ToInt32(sort.Value));
                md.url = url.Value;
                int row = role.Add(md);
                if (row > 0)
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "", " <script>parent.layer.alert(\"添加成功\", function () { parent.location.reload();});</script>");
                else
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "", " <script>parent.layer.msg(\"操作失败\"); </script>");
            }
        }
    }
}